package com.base.pig.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备总表 Mapper 接口
 * </p>
 *
 * @author yjh
 * @since 2022-04-15
 */
@Mapper
public interface ScDeviceMapper extends BaseMapper {

    // 根据点点位查设备
    @Select(" select * from  ${tableName}    where  pointid  =#{pointid}    ")
    List<Map<String,Object>> queryDeviceByPoint(@Param("tableName") String tableName,@Param("pointid") String pointid);

    //条件查询设备
    @Select(" SELECT" +
            " smID,center_X,center_Y,center_Z,company,contact,deviceID,deviceType, " +
            " deviceTypeName,installTime,maintainer,mcontact,netType,pipeType, " +
            " pointID,pointName,remarks,slavecode,smLibTileID,smUserID,sncode,slavecode " +
            " FROM  ${tableName}  ${ew.customSqlSegment} ")
    List<Map<String,Object>> queryDeviceInfoByCondition(@Param("tableName") String tableName,@Param(Constants.WRAPPER) Wrapper wrapper);


    //条件查询设备（分页）
    @Select(" SELECT" +
            " smID,center_X,center_Y,center_Z,company,contact,deviceID,deviceType, " +
            " deviceTypeName,installTime,maintainer,mcontact,netType,pipeType, " +
            " pointID,pointName,remarks,slavecode,smLibTileID,smUserID,sncode,slavecode," +
            " (   SELECT count(DEVICE_SENSOR_ID) FROM sensor_check_alarm s WHERE  t.DeviceId = s.DEVICE_SENSOR_ID AND ALARM_STATUS = '0'  AND ALARM_TYPE = '超时' )  status " +
            " FROM ${tableName} t ${ew.customSqlSegment} ")
    Page<Map<String, Object>> queryDeviceInfoByConditionPage(Page page,@Param("tableName") String tableName, @Param(Constants.WRAPPER) Wrapper wrapper); //复杂查询分页返回map


    //条件查询离线设备（分页）
    @Select({"<script>"+
            " SELECT "+
            " smID,center_X,center_Y,center_Z,company,contact,deviceID,deviceType, " +
            " deviceTypeName,installTime,maintainer,mcontact,netType,pipeType, " +
            " pointID,pointName,remarks,slavecode,smLibTileID,smUserID,sncode,slavecode," +
            " '0' status " +
            " FROM ${tableName} t   where    t.DeviceID   in " +
            "  (   SELECT   s.DEVICE_SENSOR_ID  FROM   sensor_check_alarm s   WHERE     ALARM_STATUS = '0' and   ALARM_TYPE = '超时'   ) "+
            " <if test='ew.nonEmptyOfWhere   '>"+
            " and " +
            " </if>"+
            "    ${ew.sqlSegment} "+
            "</script>"
    })
    Page<Map<String, Object>> queryDeviceInfoByConditionPageOffline(Page page,@Param("tableName") String tableName, @Param(Constants.WRAPPER) Wrapper wrapper); //复杂查询分页返回map

    //条件查询在线设备（分页）


    @Select({"<script>"+
            " SELECT "+
            " smID,center_X,center_Y,center_Z,company,contact,deviceID,deviceType, " +
            " deviceTypeName,installTime,maintainer,mcontact,netType,pipeType, " +
            " pointID,pointName,remarks,slavecode,smLibTileID,smUserID,sncode,slavecode," +
            " '1' status " +
            " FROM ${tableName} t   where    t.DeviceID  not in " +
            "  (   SELECT   s.DEVICE_SENSOR_ID  FROM   sensor_check_alarm s   WHERE     ALARM_STATUS = '0' and   ALARM_TYPE = '超时'   ) "+
            " <if test='ew.nonEmptyOfWhere   '>"+
            " and " +
            " </if>"+
            "    ${ew.sqlSegment} "+
            "</script>"
    })
    Page<Map<String, Object>> queryDeviceInfoByConditionPageOnline(Page page,@Param("tableName") String tableName, @Param(Constants.WRAPPER) Wrapper wrapper); //复杂查询分页返回map

}
